home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 442 < prev    next >
Internet Message Format  |  1996-08-06  |  4KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.std.c
  4. Subject: Re: setjmp usage question
  5. Date: 25 Feb 1996 17:43:18 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb25104318@qcd.lanl.gov>
  8. References: <4gnusq$7be@senator-bedfellow.MIT.EDU>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: tada@athena.mit.edu's message of 24 Feb 1996 21:10:50 GMT
  13.  
  14. In article <4gnusq$7be@senator-bedfellow.MIT.EDU>
  15. tada@athena.mit.edu (Michael J Zehr) writes:
  16.  
  17. <snip showing env_index is not a block scope variable>
  18. MJZ:   if (*error_code = setjmp(env[env_index++])) {
  19. <snip>
  20. MJZ:   longjmp(env[env_index], 1);
  21.  
  22. This brings in undefined behaviour, you cannot assign the result of
  23. setjmp to anything: an easy way to remember what operations are
  24. allowed is by remembering that setjmp may be implemented as a jump
  25. table instead of as `returning a value' (This is just a mnemonic, the
  26. actual rules are clearly stated in the standard). Thus its use alone,
  27. or with a single comparison or negation, as the control expression of
  28. if, while, switch etc. are perfectly valid; as is ignoring the result
  29. altogether. Any bigger expression containing it is disallowed, as is
  30. an assignment `because' it needs a returned value.
  31.  
  32. MJZ: Should this code work correctly?  In particular, can a compiler
  33. MJZ: increment env_index after longjmp is called?
  34.  
  35. I do not think so. Even though setjmp may be invoked as a macro and
  36. hence it may not have a sequence point before the call, its uses are
  37. so limited that the sequence point at the end of the complete
  38. expression (in this case the one controlling the `if') usually works
  39. well enough. So if you hadn't invoked undefined behaviour, env_index
  40. necessarily gets incremented before the branch controlled by the if is
  41. taken. 
  42.  
  43. MJZ: 
  44. MJZ: [Additional question because I'm curious:  what happens to
  45. MJZ: post-increments that are in parameters of the longjmp call?  Are they
  46. MJZ: ever executed?  I suppose the general question is where are the sequence
  47. MJZ: points when you're using setjmp and lonjmp?]
  48. MJZ: 
  49.  
  50. If there is a sequence point before the longjmp call, the increment
  51. must take place (because it is not a block scope non-volatile
  52. variable, and therefore must retain its value from the moment of the
  53. longjmp call). longjmp is described as a function, and hence if you
  54. write the call as (longjmp)(...), the function semantics definitely
  55. applies. 
  56.  
  57. I believe that the standard meant that when functions are implemented
  58. as a macro, the implementation cannot change any of the
  59. semantics. Unfortunately, this is not completely clarified in the
  60. standard. I hope that the standard will clarify one day that even when
  61. something described as a function is implemented as a macro,
  62.  
  63. 1) prototype checking and argument conversion must take place,
  64. 2) there should be a sequence point between the evaluation of the
  65.    parameters and the rest of the evaluation.
  66.  
  67. By the as-if rules, these may be elided ... but one should not have to
  68. consider these when analyzing the meaning of a program. As it stands,
  69. I think that the standard does not do so very clearly (i.e. there have
  70. been varying opinions here on comp.std.c): to be safe, one should
  71. currently use the function version instead of the macro version if one
  72. needs the sequence point.
  73.  
  74. Cheers
  75. Tanmoy
  76. --
  77. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  78. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  79. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  80. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  81. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  82. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  83.